package com.lee.litepaltest;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import org.litepal.LitePal;
import org.litepal.crud.DataSupport;
import org.litepal.tablemanager.Connector;

import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout mActivity_main;
    private EditText mEdittext, ID;
    private Button mAdd;
    private Button mDeleted;
    private Button mChange;
    private Button mCheck;
    private ListView mListview;
    
    private Bean mBean;
    private String content;
    private String id;
    
    private LitepalAdapter mAdapter;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bindViews();
        SQLiteDatabase db = Connector.getDatabase();
        LitePal.getDatabase();
        
        getDatas();
    }

    private void bindViews() {
        ID = (EditText) findViewById(R.id.editID);
        mActivity_main = (LinearLayout) findViewById(R.id.activity_main);
        mEdittext = (EditText) findViewById(R.id.edittext);
        mAdd = (Button) findViewById(R.id.add);
        mDeleted = (Button) findViewById(R.id.deleted);
        mChange = (Button) findViewById(R.id.change);
        mCheck = (Button) findViewById(R.id.check);
        mListview = (ListView) findViewById(R.id.listview);
        
        mAdd.setOnClickListener(this);
        mDeleted.setOnClickListener(this);
        mChange.setOnClickListener(this);
        mCheck.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.add:
                content = mEdittext.getText().toString().trim();
                id = ID.getText().toString().trim();
                if (!"".equals(id)){
                    Toast.makeText(this, "新增数据时不能输入id", Toast.LENGTH_SHORT).show();
                    return;
                }
                mBean = new Bean();
//                mBean.setId(id);
                mBean.setContent(content);
//                mBean.setDate("1111111111");  
                mBean.setDate(new Date());
                mBean.save();
                if (mBean.save()) {
                    Toast.makeText(this, "存储成功", Toast.LENGTH_SHORT).show();
                } else { 
                    Toast.makeText(this, "存储失败", Toast.LENGTH_SHORT).show(); 
                }
                getDatas();
                break;
            case R.id.deleted:
                content = mEdittext.getText().toString().trim();
                id = ID.getText().toString().trim();
                // 第二个参数是条件，条件可以根据自己需求设置
//                DataSupport.deleteAll(Bean.class, "id = ?", id);    //删除和输入id相同的所有数据， 
                DataSupport.deleteAll(Bean.class, "content = ?", content);
                //                DataSupport.deleteAll(Bean.class);  //删除所有
                getDatas();
                break;
            case R.id.change:
                content = mEdittext.getText().toString().trim();
                id = ID.getText().toString().trim();
                mBean = new Bean();
                mBean.setContent(content);
                mBean.updateAll("id = ?", id);
//                mBean.updateAll("id = ?");      //更新所有
                getDatas();
                break;
            case R.id.check:
                content = mEdittext.getText().toString().trim();
                id = ID.getText().toString().trim();
                getDatas();
                List<Bean> mDestList = DataSupport.where("id = ?", "1").find(Bean.class);
                Bean first =DataSupport.findFirst(Bean.class);      //第一条数据
                Bean last =DataSupport.findLast(Bean.class);        //最后一条数据
                List<Bean> list = DataSupport.select("id", id)  //查询哪几列
                        .where("content", "我")     //查询条件   
                        .order("data desc")         //排序
                        .limit(5)                   // 返回条数
                        .offset(5)                  //偏移量  主要用于进行分页展示
                        .find(Bean.class);          //哪张表
                break;
        }
    }
    
    public void getDatas(){
        List<Bean> list = DataSupport.findAll(Bean.class);  //查询所有
        if (list.size() > 0){
            mAdapter = new LitepalAdapter(list, this);
            mListview.setAdapter(mAdapter);
        }
    }
    
    
}
